local class = require 'hump.class'

StringFunction = class{
  function(self, ...)
    self.functions = {}
    self:addFunctions(...)
  end
}

function StringFunction:addFunctions(...)
  for k,v in pairs({...}) do
    self.functions[v] = v
  end
end

function StringFunction:runFunction(funcstr, object)
  local funcname = funcstr:match('%w+')
  if self.functions[funcname] then
    if object then
      funcstr = 'object:' .. funcstr
    end
    local f = loadstring('ret = ' .. funcstr)
    fenv = {__index = _G, object = object}
    setfenv(f, fenv)
    f()
    return fenv.ret
  else
    error('function ' .. funcname .. ' is not authorized. Call addFunctions(\''..funcname..'\')first')
  end
end
